﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.IO;

namespace HFAMain
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            //注册程序域，未处理的事件
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

            //参数处理
            List<string> args = new List<string>(e.Args);
            args.Add("/console"); // for debug

            //如果有/console命令，则创建控制台
            if (args.FindIndex((s) => s.Equals("/console", StringComparison.OrdinalIgnoreCase)) >= 0)
            {
                Hfabase.Util.ConsoleHelper.AllocConsole();
            }

            //初始化日志
            Hfabase.Log.Log4netHelper.InitLogCommpent();

            //初始化配置系统
            Hfabase.Configs.ConfigManager.Instance.Load();

            //Hfabase.DataBase.ConnectionManager.Instance.GetConnection();

            base.OnStartup(e);
        }

        void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            LogFatalError(e.ExceptionObject.ToString());
            MessageBox.Show("捕捉到未处理的应用程序错误\r\n请通过日志文件:" + Hfabase.Log.Log4netHelper.LOG4NET_CONFIG_FILE_PATH + "与作者联系\r\n" + e.ExceptionObject.ToString(), "系统提示", MessageBoxButton.OK, MessageBoxImage.Error);
        }

        public static void LogFatalError(string message)
        {
            try
            {
                File.AppendAllText(Hfabase.Log.Log4netHelper.LOG4NET_CONFIG_FILE_PATH, DateTime.Now.ToString() + ":" + message, System.Text.Encoding.Unicode);
            }
            catch
            {
            }
        }
    }
}
